﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class FlowStepSet : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] == null || Session["roleID"] == null)
        {
            Response.Redirect("login.aspx");
            Response.End();
        }
        if (Session["roleID"].ToString() != "1")
        {
            PanelForm.Visible = false;
        }
        else
        {
            PanelForm.Visible = true; 
        }
        if (!Page.IsPostBack)
        {
            string FlowMID = Request.QueryString["flowid"].ToString();            
            lblFlowMID.Text = FlowMID;
            BindToDdlDep();
            ddlToDep.Attributes.Add("onchange", "send_request()");
            BindToDdlName();
            BindToFlowSteps();
            txtSEQ.Text = "1";
            btnFinish.Attributes.Add("onclick", "window.location.href ='FlowModelList.aspx';");
        }

    }

    //绑定数据到DdlDep控件
    protected void BindToDdlDep()
    {
        DBOperation DBO=new DBOperation();
        string sqlString = "select DEPID,DEPNAME from DEPARTMENT where STATUS=1 order by DEPID ASC";
        SqlDataReader sdr = DBO.GetDataReader(sqlString);
        ddlToDep.DataSource = sdr;
        ddlToDep.DataTextField = "DEPNAME";
        ddlToDep.DataValueField = "DEPID";
        ddlToDep.DataBind();
        sdr.Close();
 
    }
    //绑定数据到DdlName控件
    protected void BindToDdlName()
    {
        DBOperation DBO = new DBOperation();
        string sqlString = "select TRUENAME,USERNAME from USERS where STATUS=1 and USERNAME<>'admin' and DEPID=" + ddlToDep.SelectedValue + " order by DEPID ASC";
        SqlDataReader sdr = DBO.GetDataReader(sqlString);
        ddlToUserName.DataSource = sdr;
        ddlToUserName.DataTextField = "TRUENAME";
        ddlToUserName.DataValueField = "USERNAME";
        ddlToUserName.DataBind();
        sdr.Close();

    }
    //实现部门与人员的二级联动
    protected void ddlDep_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindToDdlName();
    }
    //绑定流程步骤
    protected void BindToFlowSteps()
    {
        string sqlString = "select * from LCBZMINFO inner join USERS on LCBZMINFO.USERIDLIST=USERS.USERNAME where FLOWMID=" + lblFlowMID.Text.ToString().Trim() + " order by SEQ ASC";
        DBOperation DBO = new DBOperation();
        DataSet ds = DBO.GetDataSet(sqlString, "temp");
        Repeater1.DataSource = ds.Tables["temp"].DefaultView;
        Repeater1.DataBind();
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        //判断该步骤顺序号是否已被占用
        string stepSEQ = txtSEQ.Text.ToString().Trim();
        string FlowMID=lblFlowMID.Text.ToString().Trim();
        string CheckSQL = "select count(*) FROM LCBZMINFO where SEQ=" + stepSEQ + " and FLOWMID='" + FlowMID + "'";
        BasicClass BC = new BasicClass();
        //步骤顺序号若已被占用，提示用户
        if (BC.CheckRecordExist(CheckSQL))
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('该流程步骤序号已被占用，请更改步骤序号!');", true);
        }
         //若未被占用，添加数据库记录，提示添加成功，重新绑定已添加步骤。
        else
        {
            FlowStep AddFStep = new FlowStep();
            AddFStep.FlowMID = Convert.ToInt32(FlowMID);
            AddFStep.StepName = txtStepName.Text;
            AddFStep.UserIDList = Request.Form["ddlToUserName"].ToString();
            AddFStep.DepID = Convert.ToInt32(ddlToDep.SelectedValue.ToString());
            AddFStep.SEQ = Convert.ToInt32(stepSEQ);
            AddFStep.Content = txtStepBrief.Text;
            if (AddFStep.AddFlowStep())
            {
                txtSEQ.Text = Convert.ToString(AddFStep.SEQ + 1);
                txtStepName.Text = "";
                txtStepBrief.Text = "";
                BindToFlowSteps();
                BindToDdlDep();
                BindToDdlName();
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('步骤添加成功!');", true); 
            }
        }
    }

    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Button btnDel = (Button)e.Item.FindControl("btnDel");
            Button btnUpdate = (Button)e.Item.FindControl("btnUpdate");
            btnDel.Attributes.Add("onclick", "return confirm('你确定要删除该流程步骤吗？');");
            if(Session["roleID"].ToString()!="1")
            {
                btnDel.Text = "无权限";
                btnDel.Enabled = false;
                btnUpdate.Text = "无权限";
                btnUpdate.Enabled = false;
            }
        }
    }

    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int LCBZMID = Convert.ToInt32(e.CommandArgument);
        if (e.CommandName == "del")
        {
            FlowStep tempFS = new FlowStep(LCBZMID);
            tempFS.DelFlowStep();
            BindToFlowSteps();
        }
        if(e.CommandName=="update")
        {
            Response.Redirect("FlowStepModify.aspx?flowstepid="+LCBZMID.ToString());
        }
    }
    protected void btnFinish_Click(object sender, EventArgs e)
    {
        Response.Redirect("FlowModelList.aspx");

    }    
}
